const axios = require('axios');

// 期望的8个分类数据
const expectedCategories = [
  { name: '推荐', name_en: 'Must Try', description: '推荐菜品', description_en: 'Recommended dishes', color: '#FF6B6B', sort_order: 1 },
  { name: '促销', name_en: 'Promo', description: '促销菜品', description_en: 'Promotional dishes', color: '#4ECDC4', sort_order: 2 },
  { name: '套餐', name_en: 'Set', description: '套餐组合', description_en: 'Set meals', color: '#45B7D1', sort_order: 3 },
  { name: '主菜', name_en: 'Main Dishes', description: '主菜类菜品', description_en: 'Main course dishes', color: '#96CEB4', sort_order: 4 },
  { name: '配菜', name_en: 'Side Dishes', description: '配菜小食', description_en: 'Side dishes and appetizers', color: '#FFEAA7', sort_order: 5 },
  { name: '炸鸡', name_en: 'Fried Chicken', description: '炸鸡类菜品', description_en: 'Fried chicken dishes', color: '#DDA0DD', sort_order: 6 },
  { name: '饮品', name_en: 'Drinks', description: '各种饮品', description_en: 'Various beverages', color: '#74B9FF', sort_order: 7 },
  { name: '甜品', name_en: 'Desserts', description: '甜品类菜品', description_en: 'Dessert dishes', color: '#FD79A8', sort_order: 8 }
];

async function updateCategories() {
  try {
    console.log('获取登录token...');
    const loginResponse = await axios.post('http://localhost:3001/api/auth/login', {
      email: 'admin@example.com',
      password: '123456'
    });
    
    if (!loginResponse.data?.data?.token) {
      throw new Error('登录失败，未获取到token');
    }
    
    const token = loginResponse.data.data.token;
    console.log('Token获取成功');
    
    const headers = { 'Authorization': `Bearer ${token}` };
    
    // 获取现有分类
    console.log('\n获取现有分类...');
    const existingResponse = await axios.get('http://localhost:3001/api/menu/categories', { headers });
    const existingCategories = existingResponse.data.data || [];
    console.log(`现有分类数量: ${existingCategories.length}`);
    
    // 创建分类名称到ID的映射
    const existingMap = {};
    existingCategories.forEach(cat => {
      existingMap[cat.name] = cat.id;
    });
    
    // 更新或创建分类
    for (const expectedCat of expectedCategories) {
      const existingId = existingMap[expectedCat.name];
      
      if (existingId) {
        // 更新现有分类
        console.log(`更新分类: ${expectedCat.name}`);
        try {
          await axios.put(`http://localhost:3001/api/menu/categories/${existingId}`, {
            ...expectedCat,
            is_active: true
          }, { headers });
          console.log(`✓ 分类 "${expectedCat.name}" 更新成功`);
        } catch (error) {
          console.error(`✗ 更新分类 "${expectedCat.name}" 失败:`, error.response?.data || error.message);
        }
      } else {
        // 创建新分类
        console.log(`创建分类: ${expectedCat.name}`);
        try {
          await axios.post('http://localhost:3001/api/menu/categories', {
            ...expectedCat,
            is_active: true
          }, { headers });
          console.log(`✓ 分类 "${expectedCat.name}" 创建成功`);
        } catch (error) {
          console.error(`✗ 创建分类 "${expectedCat.name}" 失败:`, error.response?.data || error.message);
        }
      }
    }
    
    // 验证最终结果
    console.log('\n验证最终分类数据...');
    const finalResponse = await axios.get('http://localhost:3001/api/menu/categories', { headers });
    const finalCategories = finalResponse.data.data || [];
    console.log(`最终分类数量: ${finalCategories.length}`);
    
    finalCategories.forEach(cat => {
      console.log(`- ${cat.name} (${cat.name_en}) - 排序: ${cat.sort_order}`);
    });
    
  } catch (error) {
    console.error('更新分类失败:', error.response?.data || error.message);
  }
}

updateCategories();